package com.qili.controller.base;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qili.core.annotation.Log;
import com.qili.entity.base.BaseRule;
import com.qili.exception.MyException;
import com.qili.service.base.BaseRuleService;
import com.qili.util.CommonUtil;
import com.qili.util.DateUtil;
import com.qili.util.JsonUtil;
import com.qili.util.ReType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/sys/base/compileRule")
public class BaseRuleController {
    @Autowired
    BaseRuleService baseRuleService;


    @GetMapping(value = "index")
    @RequiresPermissions("baseRule:show")
    public String index(Model model, HttpServletRequest request) {
        return "/system/compileRule/index";
    }

    /**
     * @param dto
     * @param model
     * @param page
     * @param limit
     * @Author:zhangxiaopeng
     * @Description: 查询信息
     * @Date:15:52 2021/1/13
     */
    @GetMapping(value = "showList")
    @ResponseBody
    public ReType showList(BaseRule dto, Model model, int page, int limit) {
        List<BaseRule> tList = null;
        Page<BaseRule> tPage = PageHelper.startPage(page, limit);
        try {
            tList = baseRuleService.selectRuleList(dto);
        } catch (MyException e) {
            e.printStackTrace();
        }
        return new ReType(tPage.getTotal(), tList);
    }
    /**
    * @Author:zhangxiaopeng
    * @Description: 查询预案
    * @param dto
    * @param model
    * @Date:11:44 2021/1/14
    */
//    @GetMapping(value = "queryPro")
//    @ResponseBody
//    public JsonUtil queryPro(BaseRule dto, Model model) {
//        JsonUtil re = new JsonUtil();
//        BaseRule  dtonew=new BaseRule();
//        List<BaseRule> tList = baseRuleService.selectRuleList(dtonew);
//        for (int i = 0; i < tList.size(); i++) {
//            BaseRule dtoselect= tList.get(i);
//            String keywordselect=dtoselect.getKeyword();
//            if(StringUtil.isNotBlank(keywordselect)){
//                String[] key=keywordselect.split("、");
//                for (int j = 0; j < key.length; j++) {
//                    String keysz=key[j];
//                    if(StringUtil.isNotBlank(keyword)){
//                        /*变更原因中包含关键字*/
//                        int  isfind= keyword.indexOf(keysz);
//                        if(isfind>-1){
//                            dto=dtoselect;
//                            break;
//                        }
//                    }
//
//                }
//            }
//
//        }
//        re.setData(dto);
//        re.setFlag(true);
//        return re;
//    }

    /**
     * @param model
     * @param request
     * @param dto
     * @Author:zhangxiaopeng
     * @Description:
     * @Date:16:01 2021/1/13
     */
    @GetMapping(value = "toupdate")
    @RequiresPermissions("rules:edit")
    public String toupdate(Model model, HttpServletRequest request, BaseRule dto) {
        String programId = dto.getId();
        BaseRule baseProgram = baseRuleService.selectByPrimaryKey(programId);
        model.addAttribute("dto", baseProgram);
        return "/system/compileRule/update";
    }

    /**
     * @param model
     * @param request
     * @param dto
     * @Author:zhangxiaopeng
     * @Description:详情
     * @Date:20:44 2021/1/13
     */
    @GetMapping(value = "detail")
    public String detail(Model model, HttpServletRequest request, BaseRule dto) {
        String programId = dto.getId();
        BaseRule baseProgram = baseRuleService.selectByPrimaryKey(programId);
        model.addAttribute("dto", baseProgram);
        return "/system/compileRule/detail";
    }

    /**
     * 新增
     * @param model
     * @param request
     * @param dto
     * @Author:zhangxiaopeng
     * @Description:
     * @Date:16:01 2021/1/13
     */
    @GetMapping(value = "toadd")
    @RequiresPermissions("rules:add")
    public String addStage(Model model, HttpServletRequest request, BaseRule dto) {
        return "/system/compileRule/add";
    }

    /**
     * @param dto
     * @Author:zhangxiaopeng
     * @Description:添加
     * @Date:15:59 2021/1/13
     */
    @PostMapping(value = "add")
    @ResponseBody
    @RequiresPermissions("rules:add")
    public JsonUtil add(BaseRule dto) {
        JsonUtil re = new JsonUtil();
        try {
            String id = IdUtil.simpleUUID();
            BaseRule baseProgram = new BaseRule();
            BeanUtils.copyProperties(dto, baseProgram);
            baseProgram.setId(id);
            baseRuleService.insertSelective(baseProgram);
            re.setFlag(true);
            re.setMsg("操作成功");
        } catch (Exception e) {
            re.setMsg("操作失败：" + e.getMessage());
            re.setFlag(false);
            e.printStackTrace();
        }
        return re;
    }

    /**
     * @param dto
     * @Author:zhangxiaopeng
     * @Description:修改
     * @Date:15:59 2021/1/13
     */
    @PostMapping(value = "update")
    @ResponseBody
    @RequiresPermissions("rules:edit")
    public JsonUtil update(BaseRule dto) {
        JsonUtil re = new JsonUtil();
        try {
            String programId = dto.getId();
            BaseRule baseProgram = baseRuleService.selectByPrimaryKey(programId);
            BeanUtils.copyProperties(dto, baseProgram);
            baseProgram.setModifyBy(CommonUtil.getUserId());
            baseProgram.setModifyDate(DateUtil.getCurrentDateString());
            baseRuleService.updateByPrimaryKeySelective(baseProgram);
            re.setFlag(true);
            re.setMsg("操作成功");
        } catch (Exception e) {
            re.setMsg("操作失败：" + e.getMessage());
            re.setFlag(false);
            e.printStackTrace();
        }
        return re;
    }

    /**
     *
     * @Author:zhangxiaopeng
     * @Description:删除
     * @Date:16:00 2021/1/13
     */
    @ApiOperation(value = "/delete", httpMethod = "POST", notes = "删除预案")
    @Log(desc = "删除预案", type = Log.LOG_TYPE.DEL)
    @PostMapping(value = "delete")
    @ResponseBody
    @RequiresPermissions("rules:delete")
    public JsonUtil delete(BaseRule dto) {
        if (StringUtils.isEmpty(dto.getId())) {
            return JsonUtil.error("获取数据失败");
        }
        JsonUtil re = new JsonUtil();
        try {
            baseRuleService.deleteByPrimaryKey(dto.getId());
            re.setFlag(true);
            re.setMsg("操作成功");
        } catch (Exception e) {
            re.setMsg("操作失败：" + e.getMessage());
            re.setFlag(false);
            e.printStackTrace();
        }
        return re;
    }


}
